//
//  TradeOrderCenterViewController.swift
//  Exchange
//
//  Created by rui on 2018/4/25.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class TradeOrderCenterViewController: BaseViewController,TabbarHeaderViewDelegate {

    private var selectIndex = 0
    
    var headView: TabbarHeaderView?
    
    lazy var viewModel: TradeMsgViewModel =  {
        let vm = TradeMsgViewModel()
        vm.response = {[weak self](state,isSuccess,errorMsg)in
            if state == ResponseState.trade_trade_unreadMsg  {
                self?.handleRedDot()
            }
        }
        return vm
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initNavi()
        initUI()
        NotificationCenter.default.addObserver(self, selector: #selector(loadUnReadMsg), name: NSNotification.Name.Exchange.chatNeedRefresh2, object: nil)
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.loadUnReadMsg()
    }
    
    @objc func loadUnReadMsg() {
        self.viewModel.loadUnReadMsg()
    }
    
    func initUI() {
        self.automaticallyAdjustsScrollViewInsets = false
        let waitOrder = WaitOrderViewController()
        let exchangeOrder = TradeOrderViewController()
        let historyOrder = HistoryTradeOrderViewController()
        self.addChildViewController(waitOrder)
        self.addChildViewController(exchangeOrder)
        self.addChildViewController(historyOrder)
        waitOrder.view.frame = self.view.bounds
        waitOrder.view.autoresizingMask = [.flexibleHeight,.flexibleWidth]
        self.view.addSubview(waitOrder.view)
        waitOrder.didMove(toParentViewController: self)
    }
    
    func initNavi() {
        
        let headerView = TabbarHeaderView(frame: CGRect(x: 0, y: 0, width: 200, height: 44), titles: [kText("待下单"),kText("交易中"),kText("已完成")], type: .redDot)
        headerView.delegate = self
        headView = headerView
        self.navigationItem.titleView = headerView
    }
    
    // MARK: - event
    
    func tabbarHeader(headerView: TabbarHeaderView, selectIndex: Int) {
        goto(index: selectIndex)
    }
    
    ///处理未读红点
    func handleRedDot()  {
        var postions:[Bool] = []
        postions.append(!self.viewModel.waitOrder)
        postions.append(!self.viewModel.underwayTradeOrder)
        postions.append(!self.viewModel.finishTradeOrder)
        self.headView?.setRedViewsIsHidden(postions: postions)
    }
    
    func goto(index: Int) {
        let disappearVC = self.childViewControllers[self.selectIndex]
        let appearVC = self.childViewControllers[index]
        disappearVC.willMove(toParentViewController: nil)
        disappearVC.view.removeFromSuperview()
        appearVC.view.frame = self.view.bounds
        appearVC.view.autoresizingMask = [.flexibleHeight,.flexibleWidth]
        self.view.addSubview(appearVC.view)
        appearVC.didMove(toParentViewController: self)
        self.selectIndex = index

    }
}
